const dbCongif=require('../util/dbconfig')
// 1.向购物车添加商品,(首先,应该判断该商品是否在购物车中已经存在,若存在,该商品数量+1,若不存在,则将该商品加入购物车并数量设置为1)
// 1.1查询商品是否已存在
getshoopexist=(req,res)=>{
    var user_id=req.query.user_id
    var goods_id=req.query.goods_id
    var sql=`select goods_count,count(*) as count from sp_user_cart where user_id=? and goods_id=?`
    var sqlArr=[user_id,goods_id]
    var callBack=(err,data)=>{
        if(err){
            console.log("连接失败了")
        }else{
            res.send({
                "data":data
            })
        }
    }
    dbCongif.sqlConnect(sql,sqlArr,callBack)
}
// 1.2.修改商品的数量
getshoopexit=(req,res)=>{
    var user_id=req.query.user_id
    var goods_id=req.query.goods_id
    var goods_count=req.query.goods_count
    var sql=`update sp_user_cart set goods_count=${goods_count} where user_id=? and goods_id=?`
    var sqlArr=[user_id,goods_id]
    var callBack=(err,data)=>{
        if(err){
            console.log("连接失败了")
        }else{
            
            res.send({
                "data":data
            })
        }
    }
    dbCongif.sqlConnect(sql,sqlArr,callBack)
}
// 1.3.添加商品
getshoopadd=(req,res)=>{
    var user_id=req.query.user_id
    var goods_id=req.query.goods_id
    var goods_count=req.query.goods_count
    var goods_params=req.query.goods_params
    var sql=`insert into sp_user_cart (user_id,goods_id,goods_count,goods_params) values(${user_id},${goods_id},${goods_count},'${goods_params}')`
    var sqlArr=[user_id,goods_id,goods_count,goods_params]
    var callBack=(err,data)=>{
        if(err){
            console.log("连接失败了")
        }else{
            res.send({
                "data":data
            })
        }
    }
    dbCongif.sqlConnect(sql,sqlArr,callBack)
}
// 3.删除商品(通过请求servlet并根据要删除商品的唯一属性将其删除)
// 4.清空购物车(请求servlet将购物车清空)
// 5.获得商品的初始价格
// 6.获得商品的会员价格
// 7.获得优惠价格
// select * from sp_user_cart suc left join sp_goods_pics spc on suc.goods_id = spc.goods_id where user_id = ? order by created_at desc  
// 8.获得购物车中的所有商品
getshooplist=(req,res)=>{
    var user_id=req.query.user_id
    console.log(user_id)
    var sql=`select * from sp_user_cart where user_id=? order by created_at desc`
    var sqlArr=[user_id]
    var callBack=(err,data)=>{
        if(err){
            console.log("连接失败了")
        }else{
            res.send({
                "data":data
            })
        }
    }
    dbCongif.sqlConnect(sql,sqlArr,callBack)
}

module.exports={
    getshooplist,
    getshoopexist,
    getshoopexit,
    getshoopadd
}